package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.ContentQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.service.impl.TravelServiceImpl;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//专门做查询
@RestController
@RequestMapping("/contents")
public class ContentController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ServletContext servletContext;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDailyService dailyService;
    @Autowired
    private IUserService userService;

    //根据类型查询
    @GetMapping(produces="text/html")
    public void queryList(ContentQueryObject qo, HttpServletResponse response) {
        List<?> list = new ArrayList<>();
        String type = qo.getType();
        HashMap<String,Object> map = new HashMap<>();
        if("strategys".equals(type)){
            qo.setState(-2);
            map.put("strategys",strategyService.queryByKeyword(qo));
        }else if("travels".equals(qo.getType())){
            qo.setState(null);
            map.put("travels",travelService.query(qo));
        }else if("articles".equals(qo.getType())){
            map.put("articles",dailyService.query(qo));
        }else if("users".equals(qo.getType())){
            map.put("users",userService.query(qo));
        }
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        try {
            configuration.setDirectoryForTemplateLoading(new File(servletContext.getRealPath("/template")));
            Template template = configuration.getTemplate(type + ".ftl","utf-8");
            try {
                template.process(map,response.getWriter());
            } catch (TemplateException e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //返回Json数据
    @GetMapping(produces = "application/json")
    public Object queryList(ContentQueryObject qo) {
        String type = qo.getType();
        PageInfo pageInfo = null;
        if("strategys".equals(type)) {
            qo.setState(-2);
            pageInfo = strategyService.queryByKeyword(qo);
        }else if("travels".equals(qo.getType())){
            qo.setState(null);
            pageInfo = travelService.query(qo);
        }else if("articles".equals(qo.getType())){
            pageInfo  = dailyService.query(qo);
        }else if("users".equals(qo.getType())){
            pageInfo = userService.query(qo);
        }
        return pageInfo;
    }

}
